package m.jclecai.com.fragment.refreshableList;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.follow.HotPeopleInfoActivity;
import m.jclecai.com.adapter.base.DefaultViewHolderAdapter;
import m.jclecai.com.adapter.holder.DefaultViewHolder;
import m.jclecai.com.bean.Response.ResConcernList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.rounded.RoundedImageView;

/**
 * Created by Dinosa on 2017/11/24.
 */

public class MineConcernListFragment extends RefreshableListFragment {

    @Override
    public void requestData() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user", PreferenceUtil.getUserName(getActivity()));
        paramsMap.put("password",PreferenceUtil.getPassword(getActivity()));
        HttpManager.postAsyn(Constant_Url.MY_CONCERN, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络连接异常!!!");
                mPrListview.onRefreshComplete();
            }

            @Override
            public void onResponse(String response) {
                if (!TextUtils.isEmpty(response)) {
                    //这里我们对数据进行要给解析
                    ResConcernList resHitList = FastOk.get(response, ResConcernList.class);
                    if (resHitList != null && "0".equals(resHitList.getStatus())) {

                        List<ResConcernList.ConcernBean> hitList = resHitList.getRecords();
                        int totalPages = Integer.parseInt(resHitList.getPages());

                        mAdapter.addAll(hitList);

                        if (mPage < totalPages) {
                            mPage += 1;
                        }
                    } else {
                        DisPlay(resHitList.getMessage());
                    }

                } else {
                    DisPlay("网络连接异常!!!");
                }
                mPrListview.onRefreshComplete();
            }
        }, paramsMap);
    }

    @Override
    protected DefaultViewHolderAdapter createAdapter() {
        return new ConcernListAdpater(getActivity());
    }


    /**
     * 创建一个adapter
     */
    public class ConcernListAdpater extends DefaultViewHolderAdapter<ResConcernList.ConcernBean> {



        public ConcernListAdpater(Context context) {
            super(context);
        }

        @Override
        public DefaultViewHolder<ResConcernList.ConcernBean> getRealViewHolder(Context context) {
            return new ConcernViewHolder(context, R.layout.item_list_concern);
        }
    }

    public class ConcernViewHolder extends DefaultViewHolder<ResConcernList.ConcernBean> {

        @BindView(R.id.iv_user_icon)
        RoundedImageView mIvUserIcon;
        @BindView(R.id.tv_username)
        TextView mTvUsername;
        @BindView(R.id.tv_bet_history)
        TextView mTvBetHistory;
        @BindView(R.id.tv_fans_count)
        TextView mTvFansCount;
        @BindView(R.id.tv_unit_award)
        TextView mTvUnitAward;
        @BindView(R.id.tv_is_follow)
        TextView mTvIsFollow;
        @BindView(R.id.tv_follow_number)
        TextView mTvFollowNumber;

        public ConcernViewHolder(Context context, @LayoutRes int resource) {
            super(context, resource);
        }

        @Override
        public void init(final ResConcernList.ConcernBean bean, int position) {
            mTvUsername.setText(bean.getUsername());
            String military = bean.getMilitary();
            String[] split = military.split("/");
            mTvBetHistory.setText("近7天战绩:" + split[0] + "中" + split[1]);

            Glide.with(getActivity()).load(bean.getUser_face()).into(mIvUserIcon);

            mTvFansCount.setText("粉丝: "+bean.getFans());


            //这里我们判断是否可以跟单；
            if ("0".equals(bean.getUser_project_follow())) {
                mTvFollowNumber.setVisibility(View.INVISIBLE);
                mTvIsFollow.setEnabled(false);
            } else {
                mTvIsFollow.setEnabled(true);
                mTvFollowNumber.setText(bean.getFans());
                mTvFollowNumber.setVisibility(View.VISIBLE);
            }

            mTvUnitAward.setText("单位回报: "+bean.getPayback());

            setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //这里是跳转到具体的详情页面中去；
                    startActivity(HotPeopleInfoActivity.getIntent(getActivity(),bean.getUser_id()));
                }
            });

        }


    }
}
